package lan;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class receivefile {
	String path_1 = null;
	String path_2 = null;
	String Path = null;
	String arr[] = null;
	int i = 1;
	int port;
	DataInputStream din = null;
	FileOutputStream fout = null;
	Socket socket = null;

	public void setarr(String arr[]) {
		this.arr = arr;
	}

	public void setpath(String Path) {
		this.Path = Path;
	}

	public void setport(int port) {
		this.port = port;
	}

	public void receive() throws IOException, InterruptedException {
		ServerSocket server = new ServerSocket(port);
		socket = server.accept();
		din = new DataInputStream(socket.getInputStream());
		int leftLen = 0; // 鍐欐弧鏂囦欢鍚庣紦瀛樺尯涓墿浣欑殑瀛楄妭闀垮害銆�
		int bufferedLen = 0; // 褰撳墠缂撳啿鍖轰腑鐨勫瓧鑺傛暟
		int writeLen = 0; // 姣忔鍚戞枃浠堕噷鍐欏叆鐨勫瓧鑺傛暟
		long writeLens = 0; // 褰撳墠宸茬粡鍚戝崟涓枃浠堕噷鍐欏叆鐨勫瓧鑺傛�绘暟
		long totalWriteLens = 0; // 鍐欏叆鐨勬墍鏈夊瓧鑺傛暟
		byte[] inputByte = new byte[1024 * 1024];
		long length2[] = new long[1000];
		for (i = 1; i < arr.length; i++) {
			length2[i] = din.readLong();
			System.out.println(length2[i] + arr[i]);
		}
		for (i = 1; i < arr.length; i++) {
			writeLens = 0;
			// File file=new File(arr[i]);
			File file2 = new File(Path + arr[i]);
			File fileParent = file2.getParentFile();
			// 鍒ゆ柇鏄惁瀛樺湪
			if (!fileParent.exists()) {
				// 鍒涘缓鐖剁洰褰曟枃浠�
				fileParent.mkdirs();
			}
			file2.createNewFile();
			fout = new FileOutputStream(file2);
			System.out.println("寮�濮嬫帴鏀舵暟鎹�...");
			/*
			 * long sum=0; int length=-1; if(length2!=0) { while((length =
			 * din.read(inputByte))!=-1){ //fout.flush(); if(sum==length2) break;
			 * fout.write(inputByte,0,length); sum+=length; System.out.println(sum);
			 * if(sum==length2) break;
			 * 
			 * } //fout.flush(); } //fout.flush();
			 */

			while (true) {
				if (leftLen > 0) {
					bufferedLen = leftLen;
				} else {
					bufferedLen = din.read(inputByte);
				}
				if (bufferedLen == -1)
					return;
				System.out.println("readlen" + bufferedLen);
				// 鍋囪宸插啓鍏ユ枃浠剁殑瀛楄妭鏁板姞涓婄紦瀛樺尯涓殑瀛楄妭鏁板凡澶т簬鏂囦欢鐨勫ぇ灏忥紝浠呬粎鍐欏叆缂撳瓨鍖虹殑閮ㄥ垎鍐呭銆�
				if (writeLens + bufferedLen >= length2[i]) {
					leftLen = (int) (writeLens + bufferedLen - length2[i]);
					writeLen = bufferedLen - leftLen;
					fout.write(inputByte, 0, writeLen); // 鍐欏叆閮ㄥ垎
					// totalWriteLens += writeLen;
					// move(inputByte, writeLen, leftLen);
					// inputByte = Arrays.copyOfRange(inputByte, writeLen, leftLen);
					System.arraycopy(inputByte, writeLen, inputByte, 0, leftLen);
					break;
				} else {
					fout.write(inputByte, 0, bufferedLen); // 鎵�鏈夊啓鍏�
					writeLens += bufferedLen;
					// totalWriteLens += bufferedLen;

					// if (totalWriteLens >=totalSize) {
					// //mListener.report(GroupChatActivity.FAIL, null);
					// return;
					// }
					leftLen = 0;
				}
				// mListener.report(GroupChatActivity.PROGRESS,
				// (int) (totalWriteLens * 100 / totalSize));
			} // while

		} // for
		System.out.println("瀹屾垚鎺ユ敹");

		// //瀹屾垚鍚屾鏃讹紝寮瑰嚭鎴愬姛鎻愮ず
		// Alert alert = new Alert(AlertType.INFORMATION); // 瀹屾垚鍚屾鏃讹紝寮瑰嚭鎴愬姛鎻愮ず
		// alert.setTitle("Finished");
		// alert.setHeaderText("Successful synchronization");
		// alert.setContentText("Congratulations");
		// alert.showAndWait();

		fout.close();
		din.close();
		socket.close();
		server.close();

	}

}
